package com.edu.hre.struts.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreJobTitle;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.JobTitleForm;
import com.edu.hre.struts.form.admin.JobTitleListForm;

public class JobTitleListAction extends DispatchAction {
	private static final Logger logger = Logger.getLogger(JobTitleListAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "jobTitle";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);

		JobTitleListForm beanForm = (JobTitleListForm) form;
		beanForm.setCanAdd(userRoleScreen.isCanCreate());
		beanForm.setCanDelete(userRoleScreen.isCanDelete()); 
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);

		if (sortBy != null) { 
			beanForm.setAsc(!beanForm.isAsc());
			CommonUtils.sortCollection(beanForm.getListJob(), beanForm.isAsc(), sortBy);
			return mapping.findForward(ACTION_NAME);
		} else if(request.getParameter(StringConstants.BTN_DELETE) != null){
			List <JobTitleForm> listJob = beanForm.getListJob();
			for(int i = 0; i < listJob.size(); i++){
				JobTitleForm tmp = listJob.get(i);
				if(tmp.getIsSelected()){
					baseService.deleteFirstPojo(tmp.getId(), HreJobTitle.class);
				}
			}
		} 

		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		List <HreJobTitle> listJob = baseService.getPojos("isDeleted", Compare.EQUAL, false, null, null, pageSize, pageSize * (currentPage - 1), HreJobTitle.class);
		
		List<JobTitleForm> allJobTitles = new ArrayList<JobTitleForm>();
		for (HreJobTitle hreJob: listJob) {
			JobTitleForm jobForm = new JobTitleForm();
			jobForm.setId(hreJob.getId());
		    jobForm.setJobTitle(hreJob.getJobTitle());
		    jobForm.setJobDescription(hreJob.getJobDescription());
		    jobForm.setNote(hreJob.getNote());
		    allJobTitles.add(jobForm);
		}
		beanForm.setListJob(allJobTitles);

		int totalItems = baseService.countByColumns(new String[]{"isDeleted"}, new int[]{Compare.EQUAL}, new Boolean[]{false}, "id", HreJobTitle.class);
		List<HrePageInfo> listPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(totalItems));
		beanForm.setListPages(listPages);
		beanForm.setTotalItems(totalItems);
		beanForm.setTotalPage(CommonUtils.getTotalPage(totalItems));
		
		return mapping.findForward(ACTION_NAME);
	}
}
